#import "MBECubeMesh.h"

static float vertices[] =
{
    // -X
    -0.5, -0.5, -0.5, 1.0, -1.0,  0.0,  0.0, 0.0, 0.0, 0.0,
    -0.5, -0.5,  0.5, 1.0, -1.0,  0.0,  0.0, 0.0, 0.0, 1.0,
    -0.5,  0.5,  0.5, 1.0, -1.0,  0.0,  0.0, 0.0, 1.0, 1.0,
    -0.5,  0.5, -0.5, 1.0, -1.0,  0.0,  0.0, 0.0, 1.0, 0.0,
    // +X
     0.5, -0.5,  0.5, 1.0,  1.0,  0.0,  0.0, 0.0, 0.0, 0.0,
     0.5, -0.5, -0.5, 1.0,  1.0,  0.0,  0.0, 0.0, 0.0, 1.0,
     0.5,  0.5, -0.5, 1.0,  1.0,  0.0,  0.0, 0.0, 1.0, 1.0,
     0.5,  0.5,  0.5, 1.0,  1.0,  0.0,  0.0, 0.0, 1.0, 0.0,
    // -Y
    -0.5, -0.5, -0.5, 1.0,  0.0, -1.0,  0.0, 0.0, 0.0, 0.0,
     0.5, -0.5, -0.5, 1.0,  0.0, -1.0,  0.0, 0.0, 0.0, 1.0,
     0.5, -0.5,  0.5, 1.0,  0.0, -1.0,  0.0, 0.0, 1.0, 1.0,
    -0.5, -0.5,  0.5, 1.0,  0.0, -1.0,  0.0, 0.0, 1.0, 0.0,
    // + Y
    -0.5,  0.5,  0.5, 1.0,  0.0,  1.0,  0.0, 0.0, 0.0, 0.0,
     0.5,  0.5,  0.5, 1.0,  0.0,  1.0,  0.0, 0.0, 0.0, 1.0,
     0.5,  0.5, -0.5, 1.0,  0.0,  1.0,  0.0, 0.0, 1.0, 1.0,
    -0.5,  0.5, -0.5, 1.0,  0.0,  1.0,  0.0, 0.0, 1.0, 0.0,
    // -Z
     0.5, -0.5, -0.5, 1.0,  0.0,  0.0, -1.0, 0.0, 0.0, 0.0,
    -0.5, -0.5, -0.5, 1.0,  0.0,  0.0, -1.0, 0.0, 0.0, 1.0,
    -0.5,  0.5, -0.5, 1.0,  0.0,  0.0, -1.0, 0.0, 1.0, 1.0,
     0.5,  0.5, -0.5, 1.0,  0.0,  0.0, -1.0, 0.0, 1.0, 0.0,
    // +Z
    -0.5, -0.5,  0.5, 1.0,  0.0,  0.0,  1.0, 0.0, 0.0, 0.0,
     0.5, -0.5,  0.5, 1.0,  0.0,  0.0,  1.0, 0.0, 0.0, 1.0,
     0.5,  0.5,  0.5, 1.0,  0.0,  0.0,  1.0, 0.0, 1.0, 1.0,
    -0.5,  0.5,  0.5, 1.0,  0.0,  0.0,  1.0, 0.0, 1.0, 0.0,
};

static MBEIndex indices[] =
{
     3,  1,  2,  0,  1,  3,
     7,  5,  6,  4,  5,  7,
    11,  9, 10,  8,  9, 11,
    15, 13, 14, 12, 13, 15,
    19, 17, 18, 16, 17, 19,
    23, 21, 22, 20, 21, 23,
};

@implementation MBECubeMesh

@synthesize vertexBuffer=_vertexBuffer;
@synthesize indexBuffer=_indexBuffer;

- (instancetype)initWithDevice:(id<MTLDevice>) device
{
    if ((self = [super init]))
    {
        _vertexBuffer = [device newBufferWithBytes:vertices length:24 * sizeof(MBEVertex) options:0];
        _indexBuffer = [device newBufferWithBytes:indices length:36 * sizeof(MBEIndex) options:0];
    }
    return self;
}

@end
